// 函数节流
function throttle(fn, wait) {
  var oldTime = 0; // fn上次调用时间
  var waitTime = wait || 600; // 节流周期
  return function () {
    var context = this;
    var nowTime = new Date();
    if (nowTime - oldTime > waitTime) {
      // 当前时间 - 上次调用时间 > 节流周期
      fn.call(context, ...arguments);
      oldTime = nowTime; // 保存事件触发时间
    }
  };
}
// 函数防抖
function debounce(fn, wait) {
  var timer; // ?
  var waitTime = wait || 1000; // 节流周期
  return function () {
    clearTimeout(timer);
    var context = this;
    var args = arguments;
    timer = setTimeout(function () {
      // var args = arguments;
      fn.call(context, ...args);
    }, waitTime);
  };
}

// 基础库版本比较
function compareVersion(v1, v2) {
  v1 = v1.split('.');
  v2 = v2.split('.');
  const len = Math.max(v1.length, v2.length);

  while (v1.length < len) {
    v1.push('0');
  }
  while (v2.length < len) {
    v2.push('0');
  }

  for (let i = 0; i < len; i++) {
    const num1 = parseInt(v1[i]);
    const num2 = parseInt(v2[i]);

    if (num1 > num2) {
      return 1;
    } else if (num1 < num2) {
      return -1;
    }
  }

  return 0;
}
const numberTransform = (num) => {
  if (parseInt(num) > 10000) {
    num = (num / 10000).toFixed(1);
    return num + '万';
  } else {
    return num;
  }
};
// 判断是否为客服工作时间
function checkAuditTime() {
  var nowDate = new Date();
  var beginDate = new Date(nowDate);
  var endDate = new Date(nowDate);
  beginDate.setHours(8, 0, 0, 0);
  endDate.setHours(24, 0, 0, 0);
  if (
    nowDate.getTime() - beginDate.getTime() >= 0 &&
    nowDate.getTime() <= endDate.getTime()
  ) {
    return true;
  } else {
    return false;
  }
}

import { commonApi } from '../api/index';
async function uploadFile(filePath) {
  let fileUrl = '';
  let ossParams = await commonApi.getPostObjectParams();
  console.log(ossParams);
  console.log(filePath);
  // 开发者工具
  // http://tmp/wBYdNhh625FT4f4cb4c323564385b9907d5f645aeaaf.png
  let substringlen = 11;
  if (filePath.indexOf('wxfile://') > -1) {
    // 微信本地文件
    // wxfile://tmp_61d7f2811d26513b2a6b9232ad41ded9.jpg
    substringlen = 9;
  }
  let key = `avatar/image/${filePath.substring(substringlen)}`;
  wx.uploadFile({
    url: `https://file.iranboss.com`, // 开发者服务器的URL。
    filePath: filePath,
    name: 'file', // 必须填file。
    header: {
      'Content-Type': 'multipart/form-data',
    },
    formData: {
      key,
      ...ossParams,
    },
    success: async (res) => {
      if (res.statusCode === 204) {
        console.log('上传成功');
        console.log(res);
        fileUrl = 'https://file.iranboss.com/' + key;
        return fileUrl;
      }
      return fileUrl;
    },
    fail: async (err) => {
      console.log(err);
      return fileUrl;
    },
  });
}

// 校验手机号
const validatePhone = function (phone) {
  if (phone) {
    let reg = /^1[3456789]\d{9}$/;
    return reg.test(phone);
  }
  return false;
};

// 获取当天日期 年-月-日
const getCurrentDate = () => {
  const newDate = new Date()
  const currentYear = newDate.getFullYear()
  const currentMonth =(newDate.getMonth() + 1)  < 10 ? '0' + (newDate.getMonth() + 1) : (newDate.getMonth() + 1)
  const currentDay=  newDate.getDate() < 10 ? '0' + newDate.getDate() : newDate.getDate()
  return currentYear + '-' + currentMonth + '-' +currentDay
  // return  new Date().toISOString().split('T')[0]
}

// 获取当前时间 "2025-04-24 12:00:00"
function getCurrentDateTime() {
  const now = new Date();
  // 获取ISO格式的日期时间并调整时区
  const isoString = new Date(now.getTime() - now.getTimezoneOffset() * 60000).toISOString();
  // 提取并格式化
  const datePart = isoString.substring(0, 10);
  const timePart = isoString.substring(11, 19);
  return `${datePart} ${timePart}`;
}
module.exports = {
  throttle,
  debounce,
  numberTransform,
  checkAuditTime,
  compareVersion,
  uploadFile,
  validatePhone,
  getCurrentDate,
  getCurrentDateTime,
};
